package com.gzhryc.shared_device.oem.h5.agent.controllers.api.v1.team;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.gzhryc.common.NumberTools;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.common.payment.dao.db.PayAccountGroup;
import com.gzhryc.shared_device.common.payment.services.PayAccountGroupService;
import com.gzhryc.shared_device.oem.code.agent.dao.db.AgentRelationWallet;
import com.gzhryc.shared_device.oem.code.agent.services.AgentRelationWalletService;
import com.gzhryc.shared_device.oem.code.user.ERoleType;
import com.gzhryc.shared_device.oem.code.user.dao.db.UserWallet;
import com.gzhryc.shared_device.oem.code.user.services.UserWalletService;
import com.gzhryc.shared_device.oem.code.user.services.dto.UserWalletSearch;
import com.gzhryc.shared_device.oem.h5.agent.ClientAPICodeConstants;
import com.gzhryc.shared_device.oem.h5.agent.LoginUser;
import com.gzhryc.shared_device.oem.h5.agent.WebConstants;
import com.gzhryc.shared_device.oem.h5.agent.dto.api.v1.team.TeamUserWalletResponse;
import com.gzhryc.shared_device.oem.h5.agent.dto.api.v1.team.UserPayAccountWalletListRequest;
import com.gzhryc.shared_device.oem.h5.agent.dto.api.v1.team.UserPayAccountWalletListResponse;

public class TeamUserWalletController {

    @Route(value = "/agent/api/v1/team/user/wallet/info.data")
    public TeamUserWalletResponse info(HttpServletRequest request) {
        TeamUserWalletResponse apiResp = new TeamUserWalletResponse(ClientAPICodeConstants.NULL_DATA,"暂无钱包信息");
        LoginUser loginUser = WebUtils.getAttribute(request, WebConstants.ATTR_LOGIN_USER,LoginUser.class);
        if(loginUser != null && (ERoleType.Partner.index().equals(loginUser.getRoleType())
        		||  ERoleType.Merchant.index().equals(loginUser.getRoleType()))) {
            List<AgentRelationWallet> walletList = AgentRelationWalletService.self().findJoinByUserKeys(loginUser.getParentId(),loginUser.getId());
            if(walletList != null && walletList.size() > 0){
                apiResp.setCode(ClientAPICodeConstants.SUCCESS);
                apiResp.getBody().setItems(new ArrayList<>());

                Long money = 0L;
                Long withdrawnMoney = 0L;       //已提现金额
                for(AgentRelationWallet wallet : walletList) {
                    money = money + wallet.getMoney();
                    withdrawnMoney = withdrawnMoney + wallet.getWithdrawnMoney();

                    TeamUserWalletResponse.Item item = new TeamUserWalletResponse.Item();
                    item.setAccountGroupId(wallet.getAccountGroupId());
                    item.setAccountGroupName(wallet.getAccountGroupName());
                    item.setMoney(NumberTools.getMoneyStr(NumberTools.changeMoneyLi(wallet.getMoney())));
                    apiResp.getBody().getItems().add(item);
                }

                apiResp.getBody().setMoney(NumberTools.getMoneyLiStr(money));
                apiResp.getBody().setWithdrawnMoney(NumberTools.getMoneyLiStr(withdrawnMoney));
            }
        }
        return apiResp;
    }
    
    @Route(value = "/agent/api/v1/pay_account/user/wallet/list.data")
    public UserPayAccountWalletListResponse payAccountWalletList(HttpServletRequest request) {
    	UserPayAccountWalletListResponse apiResp = new UserPayAccountWalletListResponse(ClientAPICodeConstants.NULL_DATA, "暂无数据");
    	UserPayAccountWalletListRequest apiReq = WebUtils.getJsonRequest(request, UserPayAccountWalletListRequest.class);
        if (apiReq != null && apiReq.getBody().getPayAccountId() != null) {
            LoginUser loginUser = WebUtils.getAttribute(request, WebConstants.ATTR_LOGIN_USER, LoginUser.class);
            if (loginUser != null) {
            	PayAccountGroup payAccountGroup = PayAccountGroupService.self().getById(apiReq.getBody().getPayAccountId());
            	if(payAccountGroup != null && payAccountGroup.getUserId().equals(loginUser.getId())) {
            		 Integer pageNo = apiReq.getBody().getPageNo() == null?1:apiReq.getBody().getPageNo();
            		 UserWalletSearch search = new UserWalletSearch();
            		 search.setAccountGroupId(apiReq.getBody().getPayAccountId());
            		 search.setUserTrim(apiReq.getBody().getTrim());
            		 List<UserWallet> userWalletList = UserWalletService.self().findJoinBySearch(search, pageNo, WebConstants.PAGE_LIMIT);
            		 
            		 if(userWalletList != null && userWalletList.size() > 0) {
            			 apiResp.setCode(ClientAPICodeConstants.SUCCESS,null);
                         apiResp.getBody().setItems(new ArrayList<>());

            			 for(UserWallet userWallet : userWalletList) {
            				 UserPayAccountWalletListResponse.Item item = new UserPayAccountWalletListResponse.Item();
            				 item.setMoneyStr(userWallet.getMoneyStr());
            				 item.setWithdrawalMoneyStr(userWallet.getWithdrawalMoneyStr());
            				 item.setFreezeMoneyStr(userWallet.getFreezeMoneyStr());
            				 item.setLastUpdateDate(userWallet.getLastUpdateDate());
            				 item.setLastUpdateFMDate(userWallet.getLastUpdateFMDate());
            				 item.setLastUpdateWMDate(userWallet.getLastUpdateWMDate());
            				 item.setState(userWallet.getState());
            				 item.setStateName(userWallet.getStateName());
            				 item.setUserId(userWallet.getUserId());
							 item.setUserName(userWallet.getUser().getName());
							 item.setUserPhone(userWallet.getUser().getPhone());
            				 apiResp.getBody().getItems().add(item);           			 
            			 }
            		 }
            	}
            }
        }
        return apiResp;
    }
    
}
